$(document).ready(function(){
	$("#testAll").click(function(){
		$("#testDiv").append("abcd");
	});
	var valid=initValidator($("#myform"));
	$("#save").click(function(){
		if(valid.validate())alert("asdf");
	});
	
	
});

function initValidator(formSelector) {
	return $(formSelector)
			.kendoValidator(
					{
						errorTemplate: '<span class="k-widget k-tooltip">\
		                    <span class="message_ct">\
		                        <span class="arrow"></span>\
		                        <span class="message_text">#=message#</span>\
		                    </span>\
		                </span>',
						messages : {
							identity : "身份证不合法, 15位或18位",
							passport : "护照不合法",
							zipcode : '邮政编码不合法, 6位数字',
							barcode : "条形码不合法",
							areacode : '电话区号不合法, 3-4位数字',
							telephone : '电话号码不合法, 7-8位数字',
							mobilephone : '手机号不合法, 11位数字',
							required : '必填',
							number : '数字不合法',
							numberWithoutZoneBegin : "数字不能以0开头",
							numberTotalGreatThenZone : "两数之和必须大于0",
							integer : '数字必须是整数',
							at_least_one : '必须选择一个',
							customer_at_least_one : "客户组不能为空",
							greatThanNowDate : "日期必须大于当天",
							birthdate : '出生日期不合法',
							date : '日期不合法',
							name_word : "姓名有误",
							chinese_word : "必须为中文字符",
							money : "金额格式有误",
							moreThanZero : "金额不能小于0元",
							time : "时间格式有误",
							twoDateCompare : "区间有误",
							notEmpty:"不能为空!",
							routeDayNum:"必须为数字，不得超过30天！",
							rangeNumOfOneToHundred:"人数必须1-100",
							bool: '格式不对',
							gender: '性别格式不对',
							pinYin:'拼音格式不对',
							salesMan:'销售人信息不对',
							visaTourMark:"签证团号必填",
							visaProduct:"签证产品不能为空",
							abortFee:"退团费不能为空",
							numGreateThanZero:"不能小于0",
							greaterOrEqualZero: '不能小于0',
							cancelVisitor_at_least_one : "至少选择一个游客",
							destinationNum: '目的地不能为空',
							pchSearchWordNotEmpty:'搜索内容不能为空',
							visaSearchCountryNotEmpty:'请输入国家或地区'
						},
						rules : {
							notEmpty : function(input) {
								var content = $.trim($(input).val());
								if (content == undefined || content.length == 0 || content == "") {
									return false;
								}
								return true;
							},
							identity : function(input) {
								var value = $(input).val();
								return identityCodeValid(value) || value == "";
							},
							passport : function(input) {
								var value = $(input).val();
								if(value==undefined || value==""|| value.length==0)
									return true;
								return value.match(/^(E\d{8}|P\d{7}|G\d{8}|S\d{7,8}|D\d+|1[4,5]\d{7})$/);
							},
							barcode : function(input) {
								var value = $(input).val();
								return value.match(/^(AA[A-Z0-9]{8})$/);
							},
							zipcode : function(input) {
								var value = $(input).val();
								return value.match(/^\d{6}$/);
							},
							areacode : function(input) {
								var value = $(input).val();
								return value.match(/^\d{3,4}$/);
							},
							telephone : function(input) {
								var value = $(input).val();
								return value.match(/^\d{7,8}$/);
							},
							mobilephone : function(input) {
								var value = $(input).val();
								var reg = /^1[35678]\d{9}$/
								return value.match(reg) || value == "";
							},
							number : function(input) {
								var value = $(input).val();
								if(value == undefined || value=="" || value.length==0)
									return true;
								return !isNaN(value);
							},
							numberWithoutZoneBegin : function(input) {
								var value = $(input).val();
								return (!isNaN(value) && parseFloat(value).toFixed(0) >= 0);
							},
							numberTotalGreatThenZone : function(input) {
								var value = $(input).val();
								var tempValue = 0;
								if (typeof (getUpsetPrice()) != "undefined") {
									tempValue = getUpsetPrice();
								}
								return (!isNaN(value) && new Number(tempValue).add(new Number(convertMoneyToCent(value))) >= 0);
							},
							integer : function(input) {
								var value = $(input).val();
								return !isNaN(value) && value.indexOf(".") == -1;
							},
							at_least_one : function(input) {
								return $("input[name='" + $(input).prop("name") + "']:checked").length > 0;
							},
							customer_at_least_one : function(input) {
								if ($(input).hasClass("customerGroup_ckbox")) {
									return $(".customerGroup_ckbox:checked").length > 0;
								}
							},
							birthdate : function(input) {
								var value = $(input).val();
								var now = new Date();
								now.setHours(0, 0, 0, 0);
								var d = kendo.parseDate(value, "yyyy-MM-dd");
								var ipDate;
								if (!d) {
									iptDate = $(input).datepicker("getDate");
								} else {
									iptDate = new Date(d);
								}
								return value.match(/^\d{4}-\d{2}-\d{2}$/) && iptDate <= now;
							},
							greatThanNowDate : function(input) {
								var d = kendo.parseDate($(input).val(), "yyyy-MM-dd");
								var iptDate;
								if (!d) {
									iptDate = $(input).datepicker("getDate");
								} else {
									iptDate = new Date(d);
								}
								var now = new Date();
								now.setHours(0, 0, 0, 0);
								return iptDate > now || $(input).val() == "";
							},
							date : function(input) {
								var value = $.trim($(input).val());
								if(value=="") return true;
								var d1=kendo.parseDate(value,"dd/MMM/yyyy");
								var d2=kendo.parseDate(value,"yyyy-MM-dd");
								return d1 != null || d2 != null;
							},
							chinese_word : function(input) {
								var value = $.trim($(input).val());
								return value.match(/^[\u4E00-\u9FA5]+$/) || value == "";
							},
							name_word : function(input) {
								if (!input.val().match(/^[a-z| |A-Z]+/)) {
									return input.val().match(/^[\u4E00-\u9FFF|0-9|a-z|A-Z]{2,32}$/);
								} else {
									return input.val().length > 32 ? false : input.val().match(/^[a-z| |A-Z]+\/[a-z| |A-Z]+$/);
								}
							},
							money : function(input) {
								var value = $(input).val();
								if(value == undefined || value=="" || value.length==0)
									return true;
								return !isNaN(value);
							},
							moreThanZero : function(input) {
								var value = $(input).val();
								return !isNaN(value) && value >= 0;
							},
							time : function(input) {
								var value = $(input).val();
								return value.match(/^(([01]?[0-9])|(2[0-3])):[0-5]?[0-9] [A|P]M$/);
							},
							twoDateCompare : function(input) {
								var date0 = $(input).parent().find(".datepicker[twoDateCompare]:eq(0)").datepicker("getDate");
								var date1 = $(input).parent().find(".datepicker[twoDateCompare]:eq(1)").datepicker("getDate");
								if (null == date0 || "" == date0|| null == date1 || "" == date1) {
									return true;
								}
								return date0 <= date1;
							},
							routeDayNum : function(input) {
								var value = $(input).val();
								if (value == undefined || value.length == 0 || value == "")
									return false;
								if (isNaN(value))
									return false;
								var dayNum = parseInt(value);
								if (dayNum > 30 || dayNum < 1)
									return false;
								return true;
							},
							rangeNumOfOneToHundred:function(input){
								var value = $(input).val();
								var dayNum = parseInt(value);
								if (dayNum <1 || dayNum > 100)
									return false;
								return true;
							},
							bool : function(input) {
								var value = $(input).val();
								if (value == true || value == false)
									return true;
								if (value == "true" || value == "false")
									return true;
								return false;
							},
							gender : function(input) {
								var value = $(input).val();
								if (value == "F" || value == "M")
									return true;
								return false;
							},
							pinYin: function(input){
								var value=$.trim($(input).val());
								if(value==null || value==undefined || value=="")
									return true;
								return value.match(/^[A-Za-z]+\/[A-Za-z]+$/);
							},
							visaTourMark : function(input) {
								if (input.closest("tr").find("u").html() === "*"
										&& input.closest("tr").css("display") === "table-row") {
									var value=$.trim($(input).val());
									if(value==null || value==undefined || value=="")
										return false;
									return value.match(/^[A-Za-z0-9]*/);
								}
								return true;
							},
							visaProduct : function(input){
								if (input.closest("span").css("display") === "none") {
									return true;
								}
								var value = $.trim($(input).val());
								if (value == null || value == undefined || value == "")
									return false;
								return true;
							},
							abortFee : function(input) {
								if(input.css("display") === "none"){
									return true;
								}
								else{
									var value=$.trim($(input).val());
									if(value == undefined || value=="" || value.length==0)
										return false;
									return !isNaN(value);
								}
							},
							numGreateThanZero : function(input){
								var value=$(input).val();
								if(value == undefined || value=="" || value.length==0)
									return true;
								if(!isNaN(value) && new Number(value).valueOf() <=0) return false;
								return !isNaN(value);
							},
							greaterOrEqualZero: function(input){
								var value=$(input).val();
								if(value == undefined || value=="" || value.length==0)
									return true;
								if(!isNaN(value) && new Number(value).valueOf() < 0) return false;
								return !isNaN(value);
							},
							cancelVisitor_at_least_one : function(input) {
								var checkedNum = $(input).closest('form').find("input[name^='visitorIds']:checked").length
								return checkedNum > 0;
							},
							destinationNum : function(input) {
								//input保存已选的目的地数量
								//var num = $(input).val();
								var num = routeModel.route.destinationPlace.length;
								if(undefined == num || num == '' || num <= 0){
									return false;
								}
								return true;
							},
							pchSearchWordNotEmpty : function(input) {
								var content = $.trim($(input).val());
								if (content == undefined || content.length == 0 || content == "") {
									return false;
								}
								return true;
							},
							visaSearchCountryNotEmpty : function(input){
								var content = $.trim($(input).val());
								if (content == undefined || content.length == 0 || content == "") {
									return false;
								}
								return true;
							}
						},
						stopOnFirstInvalid : true
					}).data("kendoValidator");
}

